package com.zm.springmvc.controller;

import com.zm.springmvc.Person;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

//@Controller   //默认每个方法结果跳转页面
@RestController //默认每个方法返回结果自动转Json
public class IndexController {
    @RequestMapping("hello")
    String hello(String name){
        return "hello:" + name;
    }

    @RequestMapping("a/goIdx")
    public ModelAndView toIndex(ModelAndView mav){
        mav.setViewName("redirect:../index.html");
        return mav;
    }

    @RequestMapping(path = "add",params = {"a!=100,b=200"})
    public int add(int a,int b){
        return a+b;
    }

    @RequestMapping(path = "host",headers = "Host=localhost")
    public String host(){
        return "localhost:80";
    }

    @RequestMapping(path = "consume",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public String consume(){
        return "consume";
    }

    @RequestMapping(path = "produces1",produces = "text/html;charset=utf-8")
    public String produces1(){
        return "<h1>你好中国！</h1>";
    }

    @RequestMapping(path = "produces2",produces = "application/json;charset=utf-8")
    public String produces2(){
        return "<h1>你好中国！</h1>";
    }

    //手动参数映射
    @GetMapping("sub")
    public int sub(
            @RequestParam(name = "x",defaultValue = "5") int a,
            @RequestParam(value = "y",required = true) int b){
            return a - b;
    }

    @PostMapping(value = "person",produces = MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    public Person person(Person person){
        return person;
    }

    @RequestMapping("servlet")
    public List<Object> servlst(
            HttpServletRequest req,
            HttpServletResponse resp,
            HttpSession session,
            InputStream in,
            OutputStream os
    ){
        List<Object> ret  =new ArrayList<>();
        ret.add(req.toString());
        ret.add(resp.toString());
        ret.add(session.toString());
        ret.add(in.toString());
        ret.add(os==null ? "" : os.toString());

        return ret;
    }

    @GetMapping("path/{x}/{y}")
    public int path(@PathVariable("x") int a,@PathVariable(name = "y",required = true) int b){
        return a*b;
    }

    @GetMapping("headers")
    public Map<String,Object> headers(
                        @RequestHeader String Host,
                        @RequestHeader("Accept") String accept,
                        @RequestHeader(name = "Connection" ,required = false) String conn,
                        @RequestHeader(name = "abc" ,defaultValue = "100") String abc,
                        @CookieValue String name,
                        @CookieValue(name = "myage",defaultValue = "18") int age
                          ){
        Map<String,Object> ret = new HashMap<>();
        ret.put("Host",Host);
        ret.put("accept",accept);
        ret.put("Connection",conn);
        ret.put("abc",abc);
        ret.put("Host",Host);
        ret.put("name",name);
        ret.put("age",age);
        return ret;
    }

    @PostMapping("body")
    public String body(@RequestBody String body){
        System.out.println("body = " + body);
        return body;
    }

    @PostMapping("body1")
    public Person body1(@RequestBody Person body){
        System.out.println("body = " + body);
        return body;
    }

    @RequestMapping("body2")
    public Person body2(Person body){
        System.out.println("body = " + body);
        return body;
    }

}
